import { LocationQuery } from "vue-router";

export const useRouteQuery = () => {
  const redirect = ref("");
  const otherQuery = ref<LocationQuery>();
  const route = useRoute();

  const getOtherQuery = (query: LocationQuery) => {
    return Object.keys(query).reduce((memo, current) => {
      if (current !== "redirect") {
        memo[current] = query[current];
      }
      return memo;
    }, {} as LocationQuery);
  };

  watchEffect(() => {
    const query = route.query;

    // 解析数据
    if (query) {
      redirect.value = query.redirect as string;
      otherQuery.value = getOtherQuery(query);
    }
  });

  return {
    redirect,
    otherQuery
  };
};
